package com.boyinet.imesh2json.fx.controller

import com.boyinet.imesh2json.bean.Type
import com.boyinet.imesh2json.common.IdGenerator
import com.boyinet.imesh2json.common.UiUtil
import com.boyinet.imesh2json.fx.view.TypeDialogView
import com.boyinet.imesh2json.viewmodel.ProductTypesViewModel
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport
import de.felixroske.jfxsupport.FXMLController
import javafx.event.EventHandler
import javafx.fxml.FXML
import javafx.fxml.Initializable
import javafx.geometry.Pos
import javafx.scene.control.*
import javafx.scene.input.MouseEvent
import javafx.scene.layout.AnchorPane
import javafx.scene.layout.HBox
import javafx.stage.Modality
import javafx.stage.StageStyle
import javafx.util.Callback
import jfxtras.styles.jmetro.FlatAlert
import jfxtras.styles.jmetro.JMetro
import org.springframework.beans.factory.annotation.Autowired
import java.net.URL
import java.util.*

@FXMLController
class TypeViewController : Initializable {

    @FXML
    lateinit var root: AnchorPane

    @FXML
    lateinit var typeTable: TableView<Type>

    @FXML
    lateinit var unitTable: TableView<Type>

    @FXML
    lateinit var typeOperateColumn: TableColumn<Type, HBox>

    @FXML
    lateinit var unitOperateColumn: TableColumn<Type, HBox>

    @Autowired
    lateinit var productTypesViewModel: ProductTypesViewModel


    override fun initialize(location: URL?, resources: ResourceBundle?) {

        val jMetro = JMetro()
        jMetro.parent = root

        typeTable.items = productTypesViewModel.typeList

        unitTable.items = productTypesViewModel.unitList

        typeOperateColumn.cellFactory = callback()

        unitOperateColumn.cellFactory = callback()


    }

    fun onUnitNew(mouseEvent: MouseEvent) {
        openNewDialog(1)
    }

    fun onTypeNew(mouseEvent: MouseEvent) {
        openNewDialog(0)
    }

    private fun openNewDialog(t: Int) {
        with(productTypesViewModel) {
            update = false
            current.fill(Type(IdGenerator.instance.nextId()).apply {
                type = t
            })
        }
        UiUtil.showModal(TypeDialogView::class.java, Modality.APPLICATION_MODAL, "新增节点")
    }

    private fun callback() = Callback<TableColumn<Type?, HBox?>, TableCell<Type?, HBox?>> {
        object : TableCell<Type?, HBox?>() {
            override fun updateItem(item: HBox?, empty: Boolean) {
                super.updateItem(item, empty)
                if (empty) {
                    graphic = null
                    return
                }
                val type = tableView.items[index]
                val hBox = HBox()
                val edit = Button("编 辑")
                edit.onMouseClicked = EventHandler {
                    productTypesViewModel.index = index
                    with(productTypesViewModel) {
                        update = true
                        current.fill(type!!)
                    }
                    UiUtil.showModal(TypeDialogView::class.java, Modality.APPLICATION_MODAL, "编辑节点")
                }
                val delete = Button("删 除")
                delete.onMouseClicked = EventHandler {
                    val floatAlert = FlatAlert(Alert.AlertType.CONFIRMATION)
                    floatAlert.contentText = "确认删除该条目吗？"
                    JMetro().scene = floatAlert.dialogPane.scene
                    val showAndWait = floatAlert.showAndWait()
                    showAndWait.ifPresent {
                        if (it == ButtonType.OK) {
                            productTypesViewModel.delete(type!!)
                        }
                    }
                }
                hBox.alignment = Pos.CENTER
                hBox.spacing = 10.0
                hBox.children.addAll(edit, delete)
                graphic = hBox
            }
        }
    }

}